<HTML><HEAD> <!-- ----------------- Random Numbers #2 ----------------- --> <SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers /* THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com Copyright (c)1998 by Charles River Media. All Rights Reserved. This applet can only be re-used or modifed by license holders of the JavaScript Cookbook CD-ROM. Credit must be given in the source code and this copyright notice must be maintained. If you do not hold a license to the JavaScript Cookbook, you may NOT duplicate or modify this code for your own use. Use at your own risk. No warranty is given or implied of the suitability of this applet for any specific application. Neither Erica Sadun nor Charles River Media will be held responsible for any unwanted effects due to the use of this applet or any derivative. */ //------------------RANDOM NUMBERS---------------------------- var js_mult1=3141 var js_mult2=5821 var js_m1=100000000 var js_m2=10000 var js_iseed=0 var js_iseed1=0 var js_iseed2=0 // Return a Random Integer between 0 and N-1 function random(n) { if (js_iseed == 0) { now = new Date() js_iseed = now.getHours() + now.getMinutes() * 60 + now.getSeconds() * 3600 } js_iseed1 = js_iseed / js_m2 js_iseed2 = js_iseed % js_m2 var tmp = (((js_iseed2 * js_mult1 + js_iseed1 * js_mult2) % js_m2) * js_m2 + (js_iseed2 * js_mult2)) % js_m1 js_iseed = (tmp + 1) % js_m1 return (Math.floor((js_iseed/js_m1) * n)) } //------------------ARRAY CREATION---------------------------- // create an array function createArray(n) { for (var i = 0; i < n; i++) {this[i] = 0} return this } //------------------HISTOGRAMS---------------------------- // Uses little black squares to draw the lines // These squares are stored as gif files in the GRAFX folder function histo1(n) { for (var hi = 0; hi < n; hi++) {document.write("<img src='../GRAFX/UBLACK.GIF' HEIGHT=4 WIDTH=3>")} document.writeln("<br>") } // Uses X's to draw the lines function histo2(n) { for (var hi = 0; hi < n; hi++) {document.write("X")} document.writeln("<br>") } // Uses peas to draw the lines (just looks like dorky green dots) // These peas are stored as gif files in the GRAFX folder function histo3(n) { for (var hi = 0; hi < n; hi++) {document.write("<img src='../GRAFX/PEA.GIF' HEIGHT=12 WIDTH=12>")} document.writeln("<br>") } // Uses brackets to draw the lines function histo4(n) { for (var hi = 0; hi < n; hi++) {document.write("[ ]")} document.writeln("<br>") } <!-- done hiding --></SCRIPT></HEAD> <BODY bgcolor="ffffff" link="0000ff" vlink="770077"> <FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50 ALIGN = CENTER>Better Histograms</H1></FONT> <BLOCKQUOTE> <FONT SIZE=4> This script creates a distribution of 100 numbers between 0 and 9 and prints their histograms using several graphical styles. </FONT> <BR><BR> <FONT COLOR="770000"><SCRIPT> var ra = new createArray(10) for (var xi = 0; xi < 100; xi++) { var x = random(10) ra[x]++ } for (var xi = 0; xi < 10; xi++) { document.write(""+xi+": ") histo1(ra[xi]) } document.write("<br><br>") for (var xi = 0; xi < 10; xi++) { document.write(""+xi+": ") histo2(ra[xi]) } document.write("<br><br>") for (var xi = 0; xi < 10; xi++) { document.write(""+xi+": ") histo3(ra[xi]) } document.write("<br><br>") for (var xi = 0; xi < 10; xi++) { document.write(""+xi+": ") histo4(ra[xi]) } </SCRIPT></FONT> <FORM> <INPUT TYPE="button" VALUE="RE-ROLL and RELOAD" onClick="history.go(0)"> </FORM> </BLOCKQUOTE> <h5>Copyright ©1996 by Charles River Media, All Rights Reserved</h5> </BODY> </HTML>